<template>
  <div id="detail">
    <!-- 导航 -->
    <DetailNavBar class="nav-bar1" @topTitles="topTitles" ref="navBar" />
    <Scroll
      class="content"
      ref="scroll"
      @scroll="monitorPosition"
      :probeType="3"
      :pullUpLoad="true"
    >
      <!-- 轮播图 -->
      <DetailSwiper :topImages="topImages" />
      <!-- 参数区域 -->
      <DetailParameter :goods="goods" />
      <!-- 商家信息 -->
      <DetailShopInfo :shopInfo="shopInfo" />
      <!-- 商品详细 -->
      <DetailGoodsInfo :detailInfo="detailInfo" @imageLoad="imageLoad" />
      <!-- 参数详情 -->
      <DetailParamInfo :paramInfo="paramInfo" ref="param" />
      <!-- 用户评价 -->
      <DetailCommentInfo :commentInfo="commentInfo" ref="comment" />
      <!-- 推荐 -->
      <DetailRecommend :goods="recommemds" ref="recommend" />
    </Scroll>
    <!-- 底部导航 -->
    <DetailBottomBar @addCart="addToCart()" />
    <!-- 返回按钮 -->
    <DetailBackTop v-show="isShowBackTop" @click.native="backTop()" />
  </div>
</template>

<script>
import DetailNavBar from "./childrenComponents/DetailNavBar";
import DetailSwiper from "./childrenComponents/DetailSwiper";
import DetailParameter from "./childrenComponents/DetailParameter";
import DetailShopInfo from "./childrenComponents/DetailShopInfo";
import DetailGoodsInfo from "./childrenComponents/DetailGoodsInfo";
import DetailParamInfo from "./childrenComponents/DetailParamInfo";
import DetailCommentInfo from "./childrenComponents/DetailCommentInfo";
import DetailBottomBar from "./childrenComponents/DetailBottomBar";

import DetailBackTop from "components/content/backTop/BackTop";
import DetailRecommend from "components/content/goods/GoodsList";

import Scroll from "components/common/scroll/Scroll";

import { mapActions } from "vuex";

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail";

export default {
  name: "Detail",
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shopInfo: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      isShowBackTop: false,
      recommemds: [],
      topY: [],
      currentIndex: 0,
    };
  },
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailParameter,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBackTop,
    DetailRecommend,
    DetailBottomBar,
    Scroll,
  },
  created() {
    //保存iid
    this.iid = this.$route.params.iid;
    //通过iid获取详情数据
    getDetail(this.iid).then((res) => {
      const data = res.result;

      //轮播图数据
      this.topImages = data.itemInfo.topImages;
      //商品参数数据
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      //商家信息数据
      this.shopInfo = new Shop(data.shopInfo);
      //商品详情
      this.detailInfo = data.detailInfo;
      // 参数详情
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      //评论
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    //推荐的数据
    getRecommend().then((res) => {
      this.recommemds = res.data.list;
    });
  },
  methods: {
    ...mapActions(["addCart"]),

    imageLoad() {
      this.$refs.scroll.refresh();
      this.topY = [];
      this.topY.push(0);
      this.topY.push(this.$refs.param.$el.offsetTop - 44);
      this.topY.push(this.$refs.comment.$el.offsetTop - 44);
      this.topY.push(this.$refs.recommend.$el.offsetTop - 44);
      this.topY.push(Number.MAX_VALUE);
    },
    //监控滚轮
    monitorPosition(p) {
      this.isShowBackTop = -p.y > 1000;
      //获取Y值
      const positionY = -p.y;
      const length = this.topY.length;
      //滚动区域对应navBar
      for (let index = 0; index < length - 1; index++) {
        if (
          this.currentIndex !== index &&
          positionY >= this.topY[index] &&
          positionY < this.topY[index + 1]
        ) {
          this.currentIndex = index;
          this.$refs.navBar.current = index;
        }
      }
    },
    //返回顶部
    backTop() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    topTitles(index) {
      this.$refs.scroll.scrollTo(0, -this.topY[index], 500);
    },
    //加入购物车
    addToCart() {
      const product = {};
      product.img = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.realPrice = this.goods.realPrice;
      product.iid = this.iid;
      //引入mapActions写法
      this.addCart(product).then((msg) => {
        this.$toast.show(msg);
      });
      //原生写法
      // this.$store.dispatch("addCart", product).then((msg) => {
      //   console.log(msg);
      // });
    },
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 1000;
  background: #ffffff;
  height: 100vh;
}
.content {
  height: calc(100% - 44px - 49px);
  overflow: hidden;
}
.nav-bar1 {
  position: relative;
  z-index: 9999;
  background: #ffffff;
}
</style>